﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ocelot;
using Ocelot.Middleware;
using Ocelot.DependencyInjection;


namespace Ocelot_Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        // 写法一：
        //public static IWebHostBuilder CreateWebHostBuilder(string[] args) {

        //    return new WebHostBuilder()
        //    .UseKestrel()
        //    .UseContentRoot(Directory.GetCurrentDirectory())
        //    .ConfigureAppConfiguration((hostingContext, config) =>
        //    {
        //        config
        //            .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
        //            .AddJsonFile("appsettings.json", true, true)
        //            .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
        //            .AddJsonFile("ocelot.json")
        //            .AddEnvironmentVariables();
        //    })
        //    .ConfigureServices(s =>
        //    {
        //        s.AddOcelot();
        //    })
        //    .ConfigureLogging((hostingContext, logging) =>
        //    {
        //        //add your logging
        //    })
        //    .UseIISIntegration()
        //    .Configure(app =>
        //    {
        //        app.UseOcelot().Wait();
        //    }).UseStartup<Startup>();
        //}

        // 写法二：
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
               .UseKestrel()
               .UseContentRoot(Directory.GetCurrentDirectory())
               .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .AddJsonFile("appsettings.json", true, true)
                        .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
                        
                // TODO: Multiple environments and Merging configuration files
                        //.AddOcelot()
                        .AddJsonFile("ocelot.json")
                        //.AddJsonFile($"configuration.{hostingContext.HostingEnvironment.EnvironmentName}.json")
                        .AddEnvironmentVariables();
                })
                .ConfigureServices(s =>
                {
                    s.AddOcelot();
                })
                .ConfigureLogging((hostingContext, logging) =>
                {
                    //add your logging
                })
                .UseIISIntegration()
                .Configure(app =>
                {
                    app.UseOcelot().Wait();
                })
                .UseStartup<Startup>();
            
    }
}
